<?php


	define('UPL_ERROR', 0);
	define('UPL_SUCCESS', 1);
	
class ajaxupload
{
	var $file;
	var $_upload_base;
	var $_upload_dir;
	var $_error;
	var $_admin;
	var $_debug;
	var $eh;
	var $session;
	var $config;
	var $lang;
	
	function __construct($config, $eh, $session,$lang, $debug = false)
	{
		$this->file = array();
		$this->eh = $eh;
		$this->session = $session;
		$this->config = $config;
		$this->lang = $lang;
		
		$this->_admin = ($this->session->data['user_level'] == ADMIN) ? 1 : 0;
		$this->_debug = $debug;
		$this->_upload_base = $this->config['upload_base'];
		$this->_upload_dir = ($this->_admin) ? $this->_upload_base . '/'.'admin' : $this->_upload_base . '/'.'users' . '/' . $this->session->data['user_id'];
	}
	
	/**
	 * 
	 * @desc	grabs information about file
	 * @param	$uploadfile = string
	 */
	
	function process_file($uploadfile)
	{
		if ($this->eh->_errorFlag)
		{
			return ! $this->eh->_errorFlag;
		}

		$_file = $_FILES[$uploadfile];

		if (! $_file)
		{
			$error = 'no_file';
			$this->eh->message_die('ajaxupload', $error, __LINE__, __FILE__);
			return;
		}

		$_file['name'] = substr(strtolower(preg_replace('%[^\w\s\._-]+%i','',$_file['name'])),0,16);
		
		$this->file = pathinfo($_file['name']);
		
		$this->file['extension'] = strtolower($this->file['extension']); 
		
		$this->check_extension();
		
		$this->file['size'] = $_file['size'];
		$this->file['tmp_name'] = $_file['tmp_name'];
		$this->file['destination'] = $this->_upload_dir;

		unset($_file);
	}
	
	function process_upload()
	{
		if ($this->eh->_errorFlag)
		{
			return ! $this->eh->_errorFlag;
		}
		
		if (! move_uploaded_file($this->file['tmp_name'], $this->file['destination'] . '/'. $this->file['basename']))
		{

			$error = 'move';
			$this->eh->message_die('ajaxupload', $error, __LINE__, __FILE__);
			return;
		}
		
		$_success = ($this->config['success_with_link']) ? $this->lang['success_with_link'] : $this->lang['success'];
		
		if ($this->_admin)
		{
			$this->eh->message_die('upload',$this->config['docroot'].$this->_upload_dir .'/'. $this->file['basename'], "a",'','',1);
		}
		else
		{
			// @todo public uploads ?
			$this->eh->message_die('upload',$this->file['basename'], "b",'','',1);
		}
	
	}
	
	function check_extension()
	{
		if ($this->eh->_errorFlag)
		{
			return ! $this->eh->_errorFlag;
		}
		
		if (! in_array($this->file['extension'], explode(',',$this->config['allowed_upload_ext'])))
		{
			$error = 'disallow_extension';
			$this->eh->message_die('ajaxupload', $error, __LINE__, __FILE__);
			return;
		}		
	}

	function check_destination($path = '', $subpath = '')
	{
		if ($this->eh->_errorFlag)
		{
			return ! $this->eh->_errorFlag;
		}

		$this->_upload_dir = (!$path) ? $this->_upload_dir : $this->_upload_dir .'/'. $path ;
		$this->_upload_dir = (!$subpath) ? $this->_upload_dir : $this->_upload_dir .'/'. $subpath;

		$_dest = true;
		
		$this->config['recursive_folders'] = 1;
 
		if (! is_dir($this->_upload_dir))
		{
			$_dest = $this->config['recursive_folders'] ? mkdir($this->_upload_dir, 0777, true) : mkdir($this->_upload_dir, 0777);
		}
		
		if (! $_dest)
		{
			$error = 'cannot_create_folder';
			$this->eh->message_die('ajaxupload', $error, __LINE__, __FILE__);
			return;
		}
		
		$this->file['destination'] = $this->_upload_dir;
		
		unset($_dest);
	}

	
	function check_filename($filename = '')
	{
		if ($this->eh->_errorFlag)
		{
			return ! $this->eh->_errorFlag;
		}

		if($filename)
		{
			$this->file['filename'] = $filename;
			$this->file['basename'] = $filename.'.'.$this->file['extension'];			
			goto check_filename_end;
		}
		
		check_filename_rename:
		if (file_exists($this->_upload_dir .'/'. $this->file['basename']))
		{
			$this->file['filename'] = $this->file['filename'] . substr(md5(time()), 0, 4);
			$this->file['basename'] = $this->file['filename'] . '.' . $this->file['extension'];
			goto check_filename_rename;
		}
		
		check_filename_end:
	}
}


	
